/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-14
 * Time: 10:38
 */
public class Demo2 {
    private static int count=0;
    private static Object locker1 =new Object();
    private static Object locker2 =new Object();
    private static Object locker3 =new Object();


    public static void main(String[] args) {
     Thread t1=new Thread(()->{
         synchronized (locker1){
             for (int i = 0; i < 10; i++) {
                 while(count%3!=0){
                     try {
                         locker1.wait();
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }
                 System.out.print(Thread.currentThread().getName());
                 count++;
                 synchronized (locker2){
                     locker2.notify();
                 }


             }
             }

     },"A");
     Thread t2=new Thread(()->{
         for (int i = 0; i < 10; i++) {
             synchronized (locker2){
                 while(count%3!=1){
                     try {
                         locker2.wait();
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }
                 System.out.print(Thread.currentThread().getName());
                 count++;
                 synchronized (locker3){
                     locker3.notify();
                 }

             }
         }
     },"B");
     Thread t3=new Thread(()->{

             for (int i = 0; i < 10; i++) {
                 synchronized (locker3){
                 while(count%3!=2){
                     try {
                         locker3.wait();
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }
                 System.out.println(Thread.currentThread().getName());
                 count++;
                     synchronized (locker1){
                         locker1.notify();
                     }


             }
         }
     },"C");
     t1.start();
     t2.start();
     t3.start();
    }
}
